﻿using System;
using System.Collections.Generic;
using IntelligentStorWpfApp.Model;
using IntelligentStorWpfApp.Interface;
using IntelligentStorWpfApp.Services;
using System.Data;
using log4net;

namespace IntelligentStorWpfApp.Controller
{
    public class OperationLogController
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(OperationLogController));
        private OperationLogInterface opInterface = new OperationLogServices();

        public bool AddLog(OperationLog oplog)
        {
            return opInterface.AddLog(oplog);

        }

        public List<OperationLog> getAllLog()
        {
            DataTable dt = opInterface.GetAllLog();
            return formatOperationLogsToList(dt);
        }

        public List<OperationLog> getLogByUserAndTime(string UserName, string startTime, string endTime)
        {
            DataTable dt = opInterface.getLogByUserAndTime(UserName,startTime, endTime);
            return formatOperationLogsToList(dt);
        }

        private List<OperationLog> formatOperationLogsToList(DataTable dt)
        {
            List<OperationLog> listlog = new List<OperationLog>();
            try
            {
                OperationLog oplog;
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    oplog = new OperationLog();
                    oplog.ID = Convert.ToInt32(dt.Rows[i]["ID"]);
                    oplog.UserName = dt.Rows[i]["UserName"].ToString();
                    oplog.OperatTime = Convert.ToDateTime(dt.Rows[i]["OperatTime"]);
                    oplog.Actions = dt.Rows[i]["Actions"].ToString();
                    listlog.Add(oplog);
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
            return listlog;
        }

    }
}
